from pyecharts import charts
from pyecharts import options as opts
import jieba


jieba.load_userdict("custom_dict.txt")

# 人物列表（姓名+字）
characters = {
    # ===== 蜀汉 =====
    "刘备": ["刘备", "玄德", "刘玄德"],
    "关羽": ["关羽", "云长", "关云长", "关公"],
    "张飞": ["张飞", "翼德", "张翼德"],
    "诸葛亮": ["诸葛亮", "孔明", "诸葛孔明"],
    "赵云": ["赵云", "子龙", "赵子龙"],
    "马超": ["马超", "孟起", "马孟起"],
    "黄忠": ["黄忠", "汉升", "黄汉升"],
    "魏延": ["魏延", "文长"],
    "姜维": ["姜维", "伯约"],
    "庞统": ["庞统", "士元"],
    "法正": ["法正", "孝直"],
    "刘禅": ["刘禅", "公嗣", "阿斗"],

    # ===== 曹魏 =====
    "曹操": ["曹操", "孟德", "曹孟德"],
    "曹丕": ["曹丕", "子桓", "曹子桓"],
    "曹植": ["曹植", "子建", "曹子建"],
    "夏侯惇": ["夏侯惇", "元让"],
    "夏侯渊": ["夏侯渊", "妙才"],
    "张辽": ["张辽", "文远", "张文远"],
    "许褚": ["许褚", "仲康"],
    "徐晃": ["徐晃", "公明"],
    "张郃": ["张郃", "儁乂"],
    "司马懿": ["司马懿", "仲达", "司马仲达"],
    "郭嘉": ["郭嘉", "奉孝"],
    "荀彧": ["荀彧", "文若"],

    # ===== 东吴 =====
    "孙权": ["孙权", "仲谋", "孙仲谋"],
    "孙策": ["孙策", "伯符", "孙伯符"],
    "周瑜": ["周瑜", "公瑾", "周公瑾"],
    "鲁肃": ["鲁肃", "子敬"],
    "吕蒙": ["吕蒙", "子明"],
    "陆逊": ["陆逊", "伯言", "陆伯言"],
    "黄盖": ["黄盖", "公覆"],
    "甘宁": ["甘宁", "兴霸"],

    # ===== 群雄 =====
    "吕布": ["吕布", "奉先", "吕奉先"],
    "董卓": ["董卓", "仲颖"],
    "袁绍": ["袁绍", "本初", "袁本初"],
    "袁术": ["袁术", "公路"],
    "刘表": ["刘表", "景升", "刘景升"],
    "公孙瓒": ["公孙瓒", "伯圭"],
    "华雄": ["华雄"],
    "颜良": ["颜良"],
    "文丑": ["文丑"],
}

excludes = {"将军", "却说", "荆州", "二人", "不可", "不能",
            "如此", "商议", "如何", "主公", "军士", "左右",
            "军马", "引兵", "次日", "大喜", "天下", "东吴",
            "于是", "今日", "魏兵", "陛下", "一人", "都督",
            "人马", "不知", "不敢", "丞相", "汉中", "众将",
            "后主", "只见", "蜀兵", "大叫", "上马", "太守",
            "后主", "先主", "蜀兵", "后主", "只见", "蜀兵"}

# 创建逆向映射
name_map = {}
for full_name, names in characters.items():
    for name in names:
        name_map[name] = full_name
txt = open("《三国演义》原版.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)

counts = {}
for word in words:
    if len(word) == 1:
        continue
    elif word in name_map.keys():
        rword = name_map[word]
    elif "曰" in word:
        li = word.split("曰")
        if li[0] in name_map.keys():
            rword = name_map[li[0]]

    else:
        rword = word
    counts[rword] = counts.get(rword, 0) + 1


for word in excludes:
    del (counts[word])

items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
data = []
for i in range(16):
    word, count = items[i]
    data.append((word, count))
    # print("{0:<10}{1:>5}".format(word, count))

pie = charts.Pie()
pie.add("人物出场次数", data, radius=["40%", "80%"], )
pie.set_global_opts(legend_opts=opts.LegendOpts(

    # type_="scroll",
    pos_left=40,
    pos_top=50,
    orient="vertical"
    )
                    )
x = [i for i,k in data]
y = [k for i,k in data]
x.reverse()
y.reverse()
bar = (
    charts.Bar()
    .add_xaxis(x)
    .add_yaxis(
        series_name="出场次数",
        y_axis=y,
        label_opts=opts.LabelOpts(position="right")
    )
    .set_global_opts(

        legend_opts=opts.LegendOpts(is_show=False),  # 禁用条形图的独立图例
        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-30))  # 标签旋转
    )
    .reversal_axis()  # 横向条形图
)

page = charts.Page(layout=charts.Page.DraggablePageLayout)
# page.add(pie)
# page.add(bar)
# page.render("出场次数-饼图-条形图组合.html")
page.save_resize_html(source='出场次数-饼图-条形图组合.html',
                      cfg_file='chart_config.json',
                      dest="人物出场次数-饼图-条形图组合.html"
                      )
